package com.promis.widget.imgzoom;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.promis.util.ScreenUtils;
import com.promis.widget.R;
import com.promis.widget.recycler.GridSpacingItemDecoration;
import com.promis.widget.recycler.RecyclerLayoutManagerHelper;
import com.promis.widget.recycler.StaggeredSpacesItemDecoration;

import java.util.List;

/**
 * Created by jidanbo on 2018/6/7.
 */

public class MediaSquaredLayout extends LinearLayout {

    private ThumbnailView thumbnailView;

    private RecyclerView recyclerView;
    private ThumbnailAdapter adptImage;

    private FrameLayout frameLayout;
    private ThumbnailView videoThumbnailView;

    public MediaSquaredLayout(Context context) {
        super(context);
        init();
    }

    public MediaSquaredLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MediaSquaredLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MediaSquaredLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setVisibility(GONE);
        setOrientation(VERTICAL);
    }
    
    public void setImageList(List<String> imageUrlList) {
        removeAllViews();
        if (recyclerView == null) {
            recyclerView = new RecyclerView(getContext());
            LinearLayout.LayoutParams rylvLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            recyclerView.setLayoutParams(rylvLayoutParams);

            RecyclerLayoutManagerHelper.buildStaggeredGridLayoutManager(getContext(), recyclerView, 3);
//            recyclerView.addItemDecoration(new StaggeredSpacesItemDecoration(getContext(), 9));
            recyclerView.addItemDecoration(new GridSpacingItemDecoration(getContext(), 9));
            adptImage = new ThumbnailAdapter(getContext());
            recyclerView.setAdapter(adptImage);
        }

        addView(recyclerView);
        setVisibility(VISIBLE);

        adptImage.notifyDataSetChanged(imageUrlList);
    }

    public void setImageDetailList(List<String> imageUrlList) {
        removeAllViews();
        if (recyclerView == null) {
            recyclerView = new RecyclerView(getContext());
            LinearLayout.LayoutParams rylvLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            recyclerView.setLayoutParams(rylvLayoutParams);

            RecyclerLayoutManagerHelper.buildStaggeredGridLayoutManager(getContext(), recyclerView, 1);
//            recyclerView.addItemDecoration(new StaggeredSpacesItemDecoration(getContext(), 9));
            recyclerView.addItemDecoration(new GridSpacingItemDecoration(getContext(), 20));
            adptImage = new ThumbnailAdapter(getContext());
            recyclerView.setAdapter(adptImage);
        }

        addView(recyclerView);
        setVisibility(VISIBLE);

        adptImage.notifyDataSetChanged(imageUrlList);
    }

    public void setImage(String imageUrl, int width, int height) {
        removeAllViews();

        LinearLayout.LayoutParams layoutParams = null;
        if (thumbnailView == null) {
            thumbnailView = new ThumbnailView(getContext());
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            thumbnailView.setLayoutParams(layoutParams);

            thumbnailView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    FullScreenImagePreview imagePreview = new FullScreenImagePreview(getContext(), thumbnailView);
                    imagePreview.startTransform(FullScreenGridPreview.STATE_TRANSFORM_IN);
                }
            });
        } else layoutParams = (LinearLayout.LayoutParams) thumbnailView.getLayoutParams();

        if (width > height) {
            layoutParams.width = ScreenUtils.dp2px(getContext(), 200);
            layoutParams.height = (int) (((float) layoutParams.width) / width * height);
        } else if (width < height) {
            layoutParams.height = ScreenUtils.dp2px(getContext(), 200);
            layoutParams.width = (int) (((float) layoutParams.height) / height * width);
        } else {
            layoutParams.width = ScreenUtils.dp2px(getContext(), 150);
            layoutParams.height = layoutParams.width;
        }
        addView(thumbnailView);
        setVisibility(VISIBLE);

        thumbnailView.setImageUrl(imageUrl);
    }

    public void setVideo(String thumbnailUrl, final String videoUrl, int width, int height) {
        removeAllViews();

        LinearLayout.LayoutParams layoutParams = null;
        FrameLayout.LayoutParams vedioLayoutParams = null;
        if (frameLayout == null) {
            frameLayout = new FrameLayout(getContext());
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            frameLayout.setLayoutParams(layoutParams);

            videoThumbnailView = new ThumbnailView(getContext());
            vedioLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            frameLayout.addView(videoThumbnailView, vedioLayoutParams);

            ImageView imgvPlay = new ImageView(getContext());
            FrameLayout.LayoutParams playLayoutParams = new FrameLayout.LayoutParams(120, 120);
            playLayoutParams.gravity = Gravity.CENTER;
            imgvPlay.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgvPlay.setImageResource(R.mipmap.icon_video_play);
            frameLayout.addView(imgvPlay, playLayoutParams);
        } else {
            layoutParams = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
            vedioLayoutParams = (FrameLayout.LayoutParams) videoThumbnailView.getLayoutParams();
        }

        if (width > height) {
            layoutParams.width = ScreenUtils.dp2px(getContext(), 200);
            layoutParams.height = (int) (((float) layoutParams.width) / width * height);
        } else if (width < height) {
            layoutParams.height = ScreenUtils.dp2px(getContext(), 200);
            layoutParams.width = (int) (((float) layoutParams.height) / height * width);
        } else {
            layoutParams.width = ScreenUtils.dp2px(getContext(), 150);
            layoutParams.height = layoutParams.width;
        }

        vedioLayoutParams.width = layoutParams.width;
        vedioLayoutParams.height = layoutParams.height;
        frameLayout.requestLayout();
        frameLayout.invalidate();
        addView(frameLayout);
        setVisibility(VISIBLE);

        videoThumbnailView.setImageUrl(thumbnailUrl);
        videoThumbnailView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                FullScreenVideoPreview imagePreview = new FullScreenVideoPreview(getContext(), videoThumbnailView, videoUrl);
                imagePreview.startTransform(FullScreenGridPreview.STATE_TRANSFORM_IN);
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
